//
//  HomeTableViewController.swift
//  Weibo
//
//  Created by zxy on 2018/11/28.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit
import SnapKit
class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin{
            visitorV?.setupVisitorInfo(isHome: true, houseImgName: "visitordiscover_feed_image_house", tipMessage: "关注一些人，回这里看看有什么惊喜")
            return
        }
        createNavigationItem()
    }

    //导航栏左按钮点击事件-个人
    @objc func leftItemBtnClick(){
        print(#function)
    }
    
    //导航栏右按钮点击事件-二维码扫一扫
    @objc func rightItemBtnClick(){
       let sb = UIStoryboard(name: "QRCodeController", bundle: nil)
       let qrCodeVc = sb.instantiateInitialViewController()
        present(qrCodeVc!, animated: true, completion: nil)
    }
    
    private func createNavigationItem(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.setupNavigationItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftItemBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem.setupNavigationItem(imageName: "navigationbar_pop", target: self, action: #selector(rightItemBtnClick))
        
        //创建导航栏标题按钮
        let navTitleBtn: TitleButton = TitleButton.cz_textButton("极客江南 ", normalColor: UIColor.gray, imageNameNormal: "navigationbar_arrow_down", selectedImageName: "navigationbar_arrow_up")
        navigationItem.titleView = navTitleBtn
        navTitleBtn.addTarget(self, action: #selector(navTitleBtnClick(sender:)), for: .touchUpInside)
        
        //监听通知
        NotificationCenter.default.addObserver(self, selector: #selector(change), name: NSNotification.Name(rawValue: popoverWillShow), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(change), name: NSNotification.Name(rawValue: popoverWillDismiss), object: nil)
    }
    
   @objc func change(){
        //1.更换选中图片
    let titleBtn = navigationItem.titleView as! TitleButton
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
    //移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func navTitleBtnClick(sender: TitleButton){
        //2.弹出气泡控制器
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let popVc = sb.instantiateInitialViewController()
        //2.1设置转场代理
        popVc?.transitioningDelegate = popoverAniator
        //2.2设置转场模式-自定义
        //默认情况下,modal会移除之前的控制器的View，替换为当前弹出的Vie
        //自定义转场后，modal后控制器的View不会被销毁
        popVc?.modalPresentationStyle = UIModalPresentationStyle.custom
        present(popVc!, animated: true, completion: nil)
    }

    //懒加载
    lazy var popoverAniator: PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.popovetFrame = CGRect(x: 100, y: 56, width: 200, height: 200)
        return pa
    }()
}

